// pages/pallet/pallet.js
var app = getApp();
var util = require('../../util.js');
Page({

  /**
   * 页面的初始数据
   */

  data: {
    status:1,//状态
    totalpage:100,//总页码
    page:1,//当前页码
    loading:true,
    frist:true,
    content:[]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getData();
  },
  getData:function(res){
    var that = this;
    if (that.data.page <= that.data.totalpage){
      app.getLoginSession(function (session) {
        wx.request({
          url: app.api + '/wx_log_test/sys/cargo',
          data: {
            token: session,
            page: that.data.page,
            perpage: 10,
            status: that.data.status
          },
          success: function (res) {
            var res = res.data;
            if (res.msg == "OK") {
              var data = res.data;
              if (data.totalPages == 0) {
                that.setData({
                  loading: false,
                  loadtips: '暂无相关数据',
                  content: []
                })
              } else { 
                that.setData({
                  loading:false,
                  totalpage: data.totalPages,
                  content: that.data.content.concat(that.deallist(res.data.content)),
                  page:++that.data.page
                })
                console.log(that.data.page);
                if (that.data.totalpage==that.data.page-1){
                  that.setData({
                    loadtips: '暂无更多数据'
                  })
                }else{
                  that.setData({
                    loadtips: ''
                  })
                }
              }
            } else {
              that.setData({
                loading: false,
                loadtips: res.msg
              })
            }
          },
          fail:function(res){
            var res = res.data;
            that.setData({
              loading:true,
              errorTips:res.msg
            })
          }
        })
      })
    }
    
  },
  // 切换tab
  tab: function (event) {
    var that = this;
    var status = event.currentTarget.dataset.status;
    that.setData({
      page: 1,
      status: status,
      content: [],
      loading:true,
      errorTips:'',
      loadtips:''

    });
    that.getData();
  },
  //匹配司机
  match:function(res){
    var that = this;
    var id = res.currentTarget.dataset.id;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api+'/wx_log_test/driver/match',
        data:{
          token:session,
          cargoid:res.currentTarget.dataset.id,
          page:1,
          perpage:10
        },
        success:function(res){
          var res = res.data;
          if(res.msg=="OK"){
            var data = res.data;
            if(data.content.length>0){
              wx.navigateTo({
                url: '/pages/matchResult/matchResult?from=pallet_list&&id='+id,
              })
            }else{
              wx.showToast({
                title: '暂无匹配结果',
                icon:'success'
              })
            }
          }else{
            wx.showToast({
              title: '接口请求错误',
              icon: 'loading'
            })
          }
        },
        fail:function(res){
          wx.showToast({
            title: '接口请求错误',
            icon: 'loading'
          })
        }
      })
    })
  },
  //上拉加载更多
  lower:function(res){
    var that = this;
    that.getData();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (that.data.frist == true) {
      that.setData({
        frist: false
      })
    } else {
      that.setData({
        totalpage: 100,//总页码
        page: 1,//当前页码
        loading: true,
        content:[]
      })
      that.getData();
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  deallist(list) {
    if (list && Array.isArray(list)) {
      list.forEach((item) => {
        item.cargo.sendDate1 = util.formatDate(new Date(item.cargo.sendDate1), 'YYYY-MM-dd');
        console.log(item.price4Group2);
        for (var i in item.price4Group2){
          if (item.price4Group2[i].name == '默认组'){
            item.priceString = item.price4Group2[i].price;
            return false;
          }
        }
      });
      return list;
    }
  },
  //启动货盘
  runPallet:function(res){
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api + '/wx_log_test/cargo/change',
        data: {
          status: 1,
          id: res.currentTarget.dataset.id,
          token: session
        },
        success: function (res) {
          var res = res.data;
          if (res.msg == "OK") {
            wx.showToast({
              title: '货盘开启成功',
              icon: 'success',
              success:function(res){
                that.setData({
                  page: 1,
                  content: [],
                  loading: true,
                  errorTips: '',
                  loadtips: ''

                })
                that.getData();
              }
            })
          } else {
            wx.showToast({
              title: '请重试！',
              icon: 'loading'
            })
          }
        },
        fail: function (res) {
          wx.showToast({
            title: '请重试！',
            icon: 'loading'
          })
        }
      })
    })
    
  },
  //匹配司机
  matchDriver:function(res){
    var that = this;
    app.getLoginSession(function (session) {

    })
  }

});